package com.example.io.fileinputstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author 尚智江
 * @description
 * @CreateDate 2024/1/10 17:10
 */
public class ByteStreamByIn6 {
    public static void main(String[] args) throws IOException {
        /*
        JDK7： IO流中捕获异常的写法
        try():括号中写创建对象的代码，
            注意： 只有实现了AutoCloseable接口的类才能使用try-with-resources，才能在小括号中创建对象
        --------
        try(){

        }catch(){

        }
         */

        try( FileInputStream fileInputStream = new FileInputStream("C:\\Users\\28593\\Desktop\\上合文件\\Java面试就业冲刺宝典.pdf");
             FileOutputStream outputStream = new FileOutputStream("E:\\SpringBoot\\Spring-Boot\\java-io\\src\\main\\resources\\a.pdf")) {

            int len;
            byte[] bytes = new byte[1024 * 1024 * 10];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        /*

        JDK9：IO流中捕获异常的写法

         */
//        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\28593\\Desktop\\上合文件\\Java面试就业冲刺宝典.pdf");
//        FileOutputStream outputStream = new FileOutputStream("E:\\SpringBoot\\Spring-Boot\\java-io\\src\\main\\resources\\a.pdf");
//        try(fileInputStream;outputStream) {
//
//            int len;
//            byte[] bytes = new byte[1024 * 1024 * 10];
//            while ((len = fileInputStream.read(bytes)) != -1) {
//                outputStream.write(bytes, 0, len);
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//        }

    }
}
